package wisents.wicket.page;

import org.apache.wicket.Page;
import org.apache.wicket.util.tester.WicketTester;
import wisents.api.PageRepresentation;
import wisents.api.factory.Vendor;
import wisents.wicket.WicketVendor;

/**
 * Created by IntelliJ IDEA.
 * User: rabbit
 * Date: Jan 19, 2010
 * Time: 9:53:27 PM
 * To change this template use File | Settings | File Templates.
 */
public class WicketPageRepresentation implements PageRepresentation {

    private final Class<? extends Page> pageClass;
    private final Page page;
    private final WicketTester tester;

    public WicketPageRepresentation(Class<? extends Page> pageClass) {
        this.pageClass = pageClass;
        try {
            this.page = pageClass.newInstance();
        } catch (Exception e) {
            throw new IllegalArgumentException("Page cannot be initialized", e);
        }
        tester = ((WicketVendor) Vendor.getInstance()).getTester();
    }

    public WicketPageRepresentation(Page page) {
        this.page = page;
        this.pageClass = page.getPageClass();
        tester = ((WicketVendor) Vendor.getInstance()).getTester();
    }

    public void enter() {
        tester.startPage(page);
    }

    public void isRendered() {
        tester.assertRenderedPage(pageClass);
    }
}
